package com.example.asus.shixun2.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;

import com.example.asus.shixun2.R;
import com.example.asus.shixun2.Tool;


/**
 * Created by ASUS on 2019/1/1.
 */

public class MapModel {

    public boolean [][] maps;//地图
    public int boxSize;
    public Paint map_paint;//声明地图画笔
    public Paint line_paint;//声明辅助线画笔
    public Paint pause_paint;//声明游戏暂停、结束字体画笔
    Paint game_bg_paint;//声明背景图片画笔
    private Bitmap game_bg;
    public int xWidth,xHeight;//游戏区域宽高

    public MapModel(int boxSize, int xWidth, int xHeight,Context context){
        this.boxSize = boxSize;
        this.xWidth = xWidth;
        this.xHeight = xHeight;
        maps = new boolean[Tool.MAPX][Tool.MAPY];
        initPaint(context);
    }

    private void initPaint(Context context) {
        //地图画笔
        map_paint = new Paint();//初始化地图画笔
        map_paint.setColor(Color.rgb(255,102,204));
        map_paint.setAntiAlias(true);//打开抗锯齿
        //辅助线画笔
        line_paint = new Paint();//初始化辅助线画笔
        line_paint.setColor(Color.WHITE);
        line_paint.setAntiAlias(true);//打开抗锯齿
        //暂停、游戏结束画笔
        pause_paint = new Paint();//初始化暂停、游戏结束画笔
        pause_paint.setColor(Color.YELLOW);
        pause_paint.setTextSize(60);
        pause_paint.setAntiAlias(true);//打开抗锯齿
        //背景图片画笔
        game_bg_paint = new Paint();
        game_bg_paint.setAntiAlias(true);
        game_bg = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_bg);//初始化游戏背景图片
    }

    public void drawBG(Canvas canvas){
        canvas.drawBitmap(game_bg,0,0,game_bg_paint);
    }

    public void drawMaps(Canvas canvas){
        //绘制地图
        for(int x = 0;x < maps.length;x ++){
            for(int y = 0;y <maps[0].length;y ++){
                if(maps[x][y] == true){
                    canvas.drawRect(x*boxSize,y*boxSize,x*boxSize+boxSize,y*boxSize+boxSize,map_paint);
                }
            }
        }

    }

    /**绘制辅助线*/
    public void drawLine(Canvas canvas) {
        for(int x = 0;x <= maps.length;x ++){
            canvas.drawLine(x*boxSize,0,x*boxSize,xHeight,line_paint);
        }
        for(int y = 0;y <= maps[0].length;y ++){
            canvas.drawLine(0,y*boxSize,xWidth,y*boxSize,line_paint);
        }
    }

    /**绘制暂停、游戏结束状态*/
    public void drawPause(Canvas canvas,boolean isOver,boolean isPause) {
        if(isPause && !isOver){
            canvas.drawText("游戏暂停",150,400,pause_paint);
        }
        if(isOver){
            canvas.drawText("游戏结束",150,400,pause_paint);
        }
    }

    /**清楚地图*/
    public void clearMaps() {
        for(int y = 0;y < maps[0].length;y ++){
            for(int x = 0;x < maps.length;x ++){
                maps[x][y] = false;
            }
        }
    }

    /**消行处理*/
    public void cleanLine(boolean isSound,MediaPlayer mediaPlayer_delete){
        for(int y = maps[0].length - 1;y > 0 ;y -- ){
            if(checkLine(y)) {
                //执行消行
                deleteLine(y,isSound,mediaPlayer_delete);
                //从消除掉的那一行开始重新遍历
                y ++;
                Tool.addScore();
            }
        }
    }

    /**消行判断*/
    public boolean checkLine(int y){
        for(int x = 0;x < maps.length;x ++){
            //如果有一个不为true，则不能消除
            if(!maps[x][y]) return false;
        }
        return true;
    }

    /**执行消行*/
    public void deleteLine(int dy, boolean isSound , MediaPlayer mediaPlayer_delete){
//        for(int y = maps[0].length - 1;y > 0 ;y --){
            for(int x = 0;x < maps.length;x ++){
                maps[x][dy] = maps[x][dy-1];
                if(isSound) mediaPlayer_delete.start();
            }
//        }
    }
}
